package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.common.RedisConst;
import com.itheima.health.dao.*;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.*;
import com.itheima.health.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@DubboService
@Slf4j
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private SetmealCheckgroupMapper setmealCheckgroupMapper;
    @Autowired
    private CheckGroupMapper checkGroupMapper;
    @Autowired
    private CheckGroupItemMapper checkGroupItemMapper;
    @Autowired
    private CheckItemMapper checkItemMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //开启分页查询
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //创建example对象
        Example example = new Example(Setmeal.class);
        Example.Criteria criteria = example.createCriteria();
        //判断查询条件
        if (!StringUtils.isEmpty(queryPageBean.getQueryString())) {
            criteria.orLike("code", "%" + queryPageBean.getQueryString() + "%");
            criteria.orLike("name", "%" + queryPageBean.getQueryString() + "%");
            criteria.orLike("helpCode", "%" + queryPageBean.getQueryString() + "%");
        }
        Page<Setmeal> setmeals = (Page<Setmeal>) setMealMapper.selectByExample(example);

        return new PageResult(setmeals.getTotal(), setmeals.getResult());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(Setmeal setmeal, Integer[] id) {
        setMealMapper.insertSelective(setmeal);
        for (Integer integer : id) {
            SetmealCheckgroup setmealCheckgroup = new SetmealCheckgroup();
            setmealCheckgroup.setSetmealId(setmeal.getId());
            setmealCheckgroup.setCheckgroupId(integer);
             setmealCheckgroupMapper.insertSelective(setmealCheckgroup);
        }
        redisTemplate.delete(RedisConst.SETMEAL_LIST);
    }

    @Override
    public int conut(String fileName) {
        Setmeal setmeal = new Setmeal();
        setmeal.setImg(fileName);
        return setMealMapper.selectCount(setmeal);
    }

    @Override
    public List<Setmeal> findAll() {
     /*   List<Setmeal> setmealList = setMealMapper.selectAll();
        return setmealList;*/
        //查询redis中是否有套餐列表数据
        List<Setmeal> list = redisTemplate.boundListOps(RedisConst.SETMEAL_LIST).range(0, -1);
        System.out.println(list);
        //redis中没有则查询数据库
        if (list.size() ==0){
            List<Setmeal> setmealList = setMealMapper.selectAll();
            for (Setmeal setmeal : setmealList) {
                redisTemplate.boundListOps(RedisConst.SETMEAL_LIST).rightPush(setmeal);
            }
            log.info("[redis中无数据,查询数据库]");
            return setmealList;
        }else {
            log.info("[redis中有数据,直接返回]");
            return list;
        }
    }

    @Override
    public Setmeal findById(Integer id) {
        /*
        实现方法一：
         */
        //Setmeal setmeal = setMealMapper.selectByPrimaryKey(id);
        //List<SetmealCheckgroup> setmealCheckgroupList = setmealCheckgroupMapper.findBysetmealid(id);
        //List<CheckGroup> checkGroupList = new ArrayList<>();
        ////获取当前套餐对应的检查组id
        //for (SetmealCheckgroup setmealCheckgroup1 : setmealCheckgroupList) {
        //    List<CheckItem> checkItemList = new ArrayList<>();
        //    CheckGroup checkGroup1 = checkGroupMapper.selectByPrimaryKey(setmealCheckgroup1.getCheckgroupId());
        //    //根据checkgroupid到checkgroup_checkitem查询checkitemid
        //    //查询
        //    List<CheckGroupItem> checkGroupItems = checkGroupItemMapper.findbycheckgroupid(setmealCheckgroup1.getCheckgroupId());
        //    for (CheckGroupItem checkGroupItem1 : checkGroupItems) {//查出对应的每个checkitem的id
        //        Integer checkitemId = checkGroupItem1.getCheckitemId();
        //        //根据检查项id查询
        //        CheckItem checkItem = checkItemMapper.selectByPrimaryKey(checkitemId);
        //        checkItemList.add(checkItem);
        //    }
        //    checkGroup1.setCheckItems(checkItemList);
        //    checkGroupList.add(checkGroup1);
        //}
        //setmeal.setCheckGroups(checkGroupList);
        //System.out.println("=======================================");
        //System.out.println(setmeal);
        //System.out.println("=======================================");
        //
        //return setmeal;

        /*
        实现方法二：
         */
//        Setmeal setmeal = setMealMapper.selectByPrimaryKey(id);
       /* Setmeal setmeal = setMealMapper.selectByPrimaryKey(id);
        //查询检查组
        List<CheckGroup> checkGroups = checkGroupMapper.findBySetmealId(setmeal.getId());
        for (CheckGroup checkGroup : checkGroups) {
            List<CheckItem> checkItemList = checkItemMapper.findByCheckgroupId(checkGroup.getId());
            checkGroup.setCheckItems(checkItemList);
        }
        setmeal.setCheckGroups(checkGroups);
        return setmeal;*/
        //根据id查询redis是否有数据
        Setmeal redisSetmeal = (Setmeal) redisTemplate.boundValueOps(RedisConst.SETMEAL_INFO+"_"+id).get();
        if (redisSetmeal == null){
            Setmeal setmeal = setMealMapper.selectByPrimaryKey(id);
            List<CheckGroup> list = checkGroupMapper.findBySetmealId(id);
            for (CheckGroup checkGroup : list) {
                List<CheckItem> checkItemList = checkItemMapper.findByCheckgroupId(checkGroup.getId());
                checkGroup.setCheckItems(checkItemList);
            }
            setmeal.setCheckGroups(list);
            //添加到redis中,并设置30分钟有效
            redisTemplate.boundValueOps(RedisConst.SETMEAL_INFO+"_"+id).set(setmeal,30, TimeUnit.MINUTES);
            log.info("[redis中无数据,查询数据库]");
            return setmeal;
        }else {
            log.info("[redis中有数据,直接返回]");
            return redisSetmeal;
        }
    }


    @Transactional
    @Override
    public void edit(Setmeal setmeal, Integer[] id) {
        setMealMapper.updateByPrimaryKeySelective(setmeal);
//        维护中间表
        SetmealCheckgroup setmealcg=new SetmealCheckgroup();
        setmealcg.setSetmealId(setmeal.getId());
        setmealCheckgroupMapper.delete(setmealcg);
        for (Integer integer : id) {
            SetmealCheckgroup setmealCheckgroup = new SetmealCheckgroup();
            setmealCheckgroup.setSetmealId(setmeal.getId());
            setmealCheckgroup.setCheckgroupId(integer);
            setmealCheckgroupMapper.insertSelective(setmealCheckgroup);
        }
    }

    @Override
    public void delete(Integer id) {
        setMealMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Integer> findCheckGroupIds(Integer id) {
        SetmealCheckgroup setMealItem=new SetmealCheckgroup();
        setMealItem.setSetmealId(id);
        List<SetmealCheckgroup> setmealCheckgroups = setmealCheckgroupMapper.select(setMealItem);
        List<Integer> list=new ArrayList<>();
        for (SetmealCheckgroup checkgroup : setmealCheckgroups) {
            list.add(checkgroup.getCheckgroupId());
        }
        return list;
    }
}
